package com.jcf.springboot.controller.business;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.jcf.springboot.common.R;
import com.jcf.springboot.entity.business.Evaluation;
import com.jcf.springboot.entity.business.vo.UserEvalution;
import com.jcf.springboot.service.EvaluationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jjj
 * @since 2021-04-11
 */
@CrossOrigin
@RestController
@RequestMapping("/evaluation")
public class EvaluationController {

    @Autowired
    private EvaluationService evaluationService;
    @GetMapping("getListByFilm")
    public R getListByFilm(String filmId){
     List<UserEvalution> list = evaluationService.getListByFilm(filmId);
        if(list != null){
            return R.ok().data(list).message("获取影片评价列表成功");
        }
        else{
            return R.error().message("获取影片评价列表失败");
        }
    }

    @PostMapping("saveEvaluation")
    public R saveEvaluation(@RequestBody Evaluation evaluation){
        QueryWrapper<Evaluation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("member_id", evaluation.getMemberId());
        Evaluation memberId = evaluationService.getOne(queryWrapper);
        UpdateWrapper<Evaluation> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("member_id", evaluation.getMemberId());
        boolean b;
        if(memberId != null){
            b = evaluationService.update(evaluation,updateWrapper);
        }
        else{
            b = evaluationService.save(evaluation);
        }

        if(b){
            return R.ok().message("评价成功");
        }
        else{
            return R.error().message("评价失败");
        }
    }

    @DeleteMapping("removeEvaById")
    public R removeEvaById(String id){
        boolean b = evaluationService.removeById(id);
        if(b){
            return R.ok().message("删除评价成功");
        }
        else {
            return R.error().message("删除评价失败");
        }
    }

}

